/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.service.TreeService;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.modules.sys.dao.CompanyDao;
import cn.net.ecode.modules.sys.entity.Company;
import cn.net.ecode.modules.sys.entity.Office;
import cn.net.ecode.modules.sys.utils.DataScopeUtils;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 公司管理Service
 * @author 冯伟/jiaoanjian
 * @version 2014-8-19
 */
@Service
@Transactional(readOnly = true)
public class CompanyService extends TreeService<CompanyDao, Company> {

	public Company get(String companyCode) {
		return super.get(companyCode);
	}

	/**
	 * 查询公司列表(通过数据权限过滤数据)
	 */
	public List<Company> findList(Company company) {
		// 生成数据权限过滤条件
		DataScopeUtils.genSql(company, "dsf", "company_m", "a");
		return super.findList(company);
	}

	/**
	 * 不过滤数据权限的，查询公司列表
	 */
	public List<Company> findAllList(Company company) {
		return super.findList(company);
	}

	/**
	 * 查询公司列表(缓存中查询)
	 * @param isAll true：查询全部；false：查询权限范围内的公司树
	 * @return
	 */
	public List<Company> findList(Boolean isAll) {
		if (isAll != null && isAll) {
			return UserUtils.getCompanyAllList();
		} else {
			return UserUtils.getCompanyList();
		}
	}

	@Transactional(readOnly = false)
	public void save(Company company) {
		if (company.getIsNewRecord()){
			if (Global.isUseCorpModel()){
				company.setId(company.getCorpCode() + "_" + company.getViewCode());
			}else{
				company.setId(company.getViewCode());
			}
			if (get(company) != null){
				throw getValidationIdExistsException();
			}
		}
		super.save(company);
		//重新绑定组织和公司之间的关系
		dao.deleteOfficesOfCompany(company);
		if (company.getOffice().getOfficeCode() != null && StringUtils.isNotBlank(company.getOffice().getOfficeCode())) {
			String officeCodes[] = company.getOffice().getOfficeCode().split(",");
			for (int i = 0; i < officeCodes.length; i++) {
				Company company1 = new Company();
				Office office = new Office();
				office.setId(officeCodes[i]);
				company1.setCompanyCode(company.getCompanyCode());
				company1.setOffice(office);
				dao.insertOfficesOfCompany(company1);
			}
		}
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	@Transactional(readOnly = false)
	public void delete(Company company) {
		super.delete(company);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	/**
	 * 获取当前机构下的启用机构列表
	 * @param office
	 * @return
	 */
	public List<Company> getEnabledList(Company company) {
		return dao.getEnabledList(company);
	}

	/**
	 * 停用当前节点
	 */
	@Transactional(readOnly = false)
	public void disable(Company company) {
		dao.disable(company);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	/**
	 * 停用当前节点和子节点
	 */
	@Transactional(readOnly = false)
	public void disableByGroup(Company company) {
		dao.disableByGroup(company);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	/**
	 * 启用当前节点
	 */
	@Transactional(readOnly = false)
	public void enable(Company company) {
		dao.enable(company);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	/**
	 * 启用当前节点和子节点
	 */
	@Transactional(readOnly = false)
	public void enableByGroup(Company company) {
		dao.enableByGroup(company);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_LIST);
		UserUtils.removeCache(UserUtils.CACHE_COMPANY_ALL_LIST);
	}

	/**
	 * 获取组织机构
	 */
	@Transactional(readOnly = true)
	public Company getOffices(Company company) {
		String officeCodes = "";
		String officeNames = "";
		List<Company> list = dao.selectOfficesOfCompany(company);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Company company1 = list.get(i);
				officeCodes = officeCodes + company1.getOffice().getOfficeCode() + ",";
				officeNames = officeNames + company1.getOffice().getOfficeName() + ",";
			}
			officeCodes = officeCodes.substring(0, officeCodes.length() - 1);
			officeNames = officeNames.substring(0, officeNames.length() - 1);
			Office office = new Office();
			office.setOfficeCode(officeCodes);
			office.setOfficeName(officeNames);
			company.setOffice(office);
		}
		return company;
	}

	/**
	 * 根据组织机构编码查询所属公司信息
	 * @param company
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Company> findCompanyByOfficeCode(Company company) {
		return dao.findCompanyByOfficeCode(company);
	}

}